package cn.springcloud.fix.demo.thread;

public class ThreadInterruptSample2 implements Runnable {

    private volatile boolean stop = false;

    public static void main(String[] args) throws InterruptedException {
        ThreadInterruptSample2 runnable = new ThreadInterruptSample2();

        System.out.println("程序开始执行....");
        Thread t = new Thread(runnable);
        t.start();

        Thread.sleep(3000);
        System.out.println("主线程请求中断子线程....");

        //设置中断信号量
        synchronized (runnable) {
            runnable.stop = true;
        }
        Thread.sleep(3000);
        System.out.println("主线程结束....");
    }

    @Override
    public void run() {
        while (!stop) {
            long startTime = System.currentTimeMillis();
            System.out.println("run....");
            while (System.currentTimeMillis() - startTime < 1000) {
            }
        }
        System.out.println("线程中断....");
    }
}
